package com.jeez.module.sys.controller;

import com.jeez.common.domain.RestResult;
import com.jeez.common.web.rest.BaseRestController;
import com.jeez.module.sys.entity.SysUser;
import com.jeez.module.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Felix
 * @date: 2018-7-1
 * @version: v1.0
 * @description:
 */
@RestController
@RequestMapping(value = "/sys/user")
public class UserController extends BaseRestController {

    @Autowired
    private UserService userService;

    /**
     * 保存用户
     * @param model
     * @return
     */
    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ResponseBody
    public RestResult save(SysUser model) {
        RestResult result = RestResult.getSuccess();
        try {
            SysUser user = this.userService.save(model);
            result.setData(user);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            result.setStatusCode(-1);
        }
        return result;
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return
     */
    @RequestMapping(value="/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public RestResult delete(@PathVariable(value = "id") Long id) {
        RestResult result = RestResult.getSuccess();
        try {
            SysUser user = this.userService.findById(id);
            if (null != user && null != user.getId()) {
                this.userService.logicDelete(user);
            } else {
                result.setSuccess(false);
                result.setStatusCode(0);
                result.setMessage("用户不存在！");
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            result.setStatusCode(-1);
        }
        return result;
    }

    /**
     * 修改用户
     * @param model 用户
     * @return
     */
    @RequestMapping(value="update", method = RequestMethod.POST)
    @ResponseBody
    public RestResult delete(SysUser model) {
        RestResult result = RestResult.getSuccess();
        try {
            SysUser user = this.userService.findById(model.getId());
            if (null != user && null != user.getId()) {
                this.userService.update(model);
            } else {
                result.setSuccess(false);
                result.setStatusCode(0);
                result.setMessage("用户不存在！");
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            result.setStatusCode(-1);
        }
        return result;
    }

    /**
     * 根据Id查找用户
     * @param id 用户ID
     * @return
     */
    @RequestMapping(value="find/{id}", method = RequestMethod.GET)
    @ResponseBody
    public SysUser findById(@PathVariable(value = "id") Long id) {
        SysUser user = this.userService.findById(id);
        return user;
    }

    /**
     * 根据Id查找用户
     * @param userCode 用户登录名称
     * @return
     */
    @RequestMapping(value="findByCode/{userCode}")
    @ResponseBody
    public SysUser findByCode(@PathVariable(value = "userCode") String userCode) {
        SysUser user = this.userService.findByUserCode(userCode);
        return user;
    }

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value="listUser")
    @ResponseBody
    public Page<SysUser> listUser(@RequestParam(value="pageIndex", required=false, defaultValue="0") int pageIndex,
                                  @RequestParam(value="pageSize", required=false, defaultValue="10") int pageSize,
                                  Model model) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(pageIndex, pageSize, sort);

        SysUser user = new SysUser();
        Page<SysUser> page = null;
        page = this.userService.listUser(user, pageable);

        return page;
    }


}
